package com.leenmvc.core.utils.logger;

import com.leenmvc.core.security.entity.core.SysLogError;
import com.leenmvc.core.security.entity.core.SysLogOperation;
import com.leenmvc.core.security.entity.core.SysLogSql;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class LogEntityPool {

    private static Queue<SysLogOperation> sysLogOperationQueue = new ArrayBlockingQueue(1000);

    private static Queue<SysLogSql> sysLogSqlQueue = new ArrayBlockingQueue(100000);

    private static Queue<SysLogError> sysLogErrorQueue = new ArrayBlockingQueue(1000);

    static {
        for(int i = 0; i < 1000; i++) {
            sysLogOperationQueue.add(new SysLogOperation());
            sysLogSqlQueue.add(new SysLogSql());
            sysLogErrorQueue.add(new SysLogError());
        }
    }

    /**
     * 获取可用的countWrapper
     * @return
     */
    public static synchronized SysLogOperation getLogOperation(Class<?> cls) {
        SysLogOperation log = null;
        while(true) {
            if(sysLogOperationQueue.size() > 0) {
                log = sysLogOperationQueue.poll();
                break;
            }
        }
        return log;
    }

    /**
     * 获取可用的countWrapper
     * @return
     */
    public static synchronized SysLogSql getLogSql() {
        SysLogSql log = null;
        while(true) {
            if(sysLogSqlQueue.size() > 0) {
                log = sysLogSqlQueue.poll();
                break;
            }
        }
        return log;
    }

    /*
     * 关闭在用的wrapper
     *
     * @param wrapper
     */
    public static void closeLogSql(SysLogSql log) {
        //LogFactory.log(Level.INFO, "查询 sysLogSqlQueue  关闭前：" + sysLogSqlQueue.size(), LogType.WRAPPER);
        sysLogSqlQueue.add(log.clean());
        //LogFactory.log(Level.INFO, "查询 sysLogSqlQueue 关闭后：" + sysLogSqlQueue.size(), LogType.WRAPPER);
    }

}
